<?php
include_once( "common.php" );
checkLogin( STATUS_GUEST );
outputHeader();

# User needs proper status to add bugs
if ( hasStatus( STATUS_USER ) )
{
?>
<div class = "header">Add bug</div>
<form action = "addBug.php" method = "post">
<table>
<tr>
<td>Project</td><td><?php outputField( "Project", "project" ); ?></td>
<td>Assigned</td><td><?php outputField( "User", "assigned", "None" ); ?></td>
<td>Priority</td><td><?php outputField( "Priority", "priority", "Normal" ); ?></td>
<td>Status</td><td><?php outputField( "Status", "status", "New" ); ?></td>
</tr>
<tr><td>Description</td><td colspan = "7"><textarea name = "description" rows = "3" cols = "40"></textarea></td></tr>
<tr><td></td><td colspan = "7"><input type = "submit" value = "File bug report" /></td></tr>
</table>
</form>
<?php
}
?>
<div class = "header">View bugs</div>
<?php
# Which page are we showing?
$limit = " LIMIT 0, " . BUGS_PER_PAGE;
if ( ! isValidPage( $_GET["page"] ) )
    $_GET["page"] = 1;
else
{
    $limit = " LIMIT " . ( $_GET["page"] - 1 ) * BUGS_PER_PAGE . ", " . BUGS_PER_PAGE;
}

outputPageLinks();
?>
<table>
<tr>
<?php
print "<th><a href = 'main.php?" . getFullLink( "", "id", "" ) . "'>ID</a></th>";
print "<th><a href = 'main.php?" . getFullLink( "", "project", "" ) . "'>Project</a></th>";
print "<th><a href = 'main.php?" . getFullLink( "", "description", "" ) . "'>Description</a></th>";
print "<th><a href = 'main.php?" . getFullLink( "", "assigned", "" ) . "'>Assigned</a></th>";
print "<th><a href = 'main.php?" . getFullLink( "", "priority", "" ) . "'>Priority</a></th>";
print "<th><a href = 'main.php?" . getFullLink( "", "status", "" ) . "'>Status</a></th>";
?>
<th>Changes</th>
<th>Comments</th>
</tr>
<?php
# Check sorting
$sort = "Bug.priority DESC, Bug.assigned, Bug.status";
switch ( $_GET["sort"] )
{
    case "id":
        $sort = "Bug.id";
        break;
    case "project":
        $sort = "Project.name, Bug.priority DESC, Bug.status";
        break;
    case "description":
        $sort = "Bug.description";
        break;
    case "assigned":
        $sort = "Bug.assigned, Bug.priority DESC, Bug.status";
        break;
    case "priority":
        $sort = "Bug.priority DESC, Bug.assigned, Bug.status";
        break;
    case "status":
        $sort = "Bug.status, Bug.priority DESC";
        break;
}

# Showing closed bugs?
$show = " AND Status.name != 'Closed'";
if ( isset( $_GET["show"] ) )
    $show = "";

# Get all bugs
$result = db_query( "SELECT Bug.id, Project.name AS project, Bug.assigned AS assigned, Priority.name AS priority, Priority.color AS color, Status.name AS status, Bug.description FROM Bug, Project, Priority, Status, UserProject WHERE Project.id = Bug.project AND Priority.id = Bug.priority AND UserProject.user = {$_SESSION["user"]} AND UserProject.project = Project.id AND Status.id = Bug.status{$show} ORDER BY {$sort}{$limit}" );

while ( $row = db_fetch_object( $result ) )
{
    # Get the assigned user
    $row->assigned = getNameByID( "User", $row->assigned );

    # Get the number of changes since the user last logged in
    $result_changes = db_query( "SELECT COUNT( Log.id ) AS changes FROM Log, User WHERE User.id = {$_SESSION["user"]} AND Log.bug = {$row->id} AND Log.initial = 0 AND Log.timestamp > User.login" );
    $row_changes = db_fetch_object( $result_changes );
    $changes_new = $row_changes->changes != 0 ? $row_changes->changes : "-";

    # Get the total number of changes
    $result_changes = db_query( "SELECT COUNT( Log.id ) AS changes FROM Log WHERE Log.bug = {$row->id} AND Log.initial = 0" );
    $row_changes = db_fetch_object( $result_changes );
    $changes_total = $row_changes->changes != 0 ? $row_changes->changes : "-";

    # Get the number of comments since the user last logged in
    $result_comments = db_query( "SELECT COUNT( Log.id ) AS comments FROM Log, User WHERE User.id = {$_SESSION["user"]} AND Log.bug = {$row->id} AND Log.comment != '' AND Log.timestamp > User.login" );
    $row_comments = db_fetch_object( $result_comments );
    $comments_new = $row_comments->comments != 0 ? $row_comments->comments : "-";
 
    # Get the total number of comments
    $result_comments = db_query( "SELECT COUNT( Log.id ) AS comments FROM Log WHERE Log.bug = {$row->id} AND Log.comment != ''" );
    $row_comments = db_fetch_object( $result_comments );
    $comments_total = $row_comments->comments != 0 ? $row_comments->comments : "-";

    # Truncate the description
    if ( strlen( $row->description ) > DESCRIPTION_LENGTH )
    {
        $row->description = substr( $row->description, 0, DESCRIPTION_LENGTH ) . "...";
    }
    $row->description = stripslashes( $row->description );

    if ( $row->status == "Closed" )
    {
        $row->color = "EEEEEE";
    }

    print "<tr style = 'background-color:#{$row->color}'><td><a href = 'viewBug.php?id={$row->id}'>{$row->id}</a></td><td>{$row->project}</td><td><a href = 'viewBug.php?id={$row->id}'>{$row->description}</a></td><td>{$row->assigned}</td><td>{$row->priority}</td><td>{$row->status}</td><td>{$changes_new} / {$changes_total}</td><td>{$comments_new} / {$comments_total}</td></tr>\n";
}

print "</table>\n";
outputPageLinks();
outputFooter();
?>
